package org.jeecg.modules.demo.znnqjc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.znnqjc.entity.ZwglZdfxfb;
import org.jeecg.modules.demo.znnqjc.entity.ZwglZdfxzb;
import org.jeecg.modules.demo.znnqjc.mapper.ZwglZdfxfbMapper;
import org.jeecg.modules.demo.znnqjc.mapper.ZwglZdfxzbMapper;
import org.jeecg.modules.demo.znnqjc.service.IZwglZdfxfbService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 诊断分析附表
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class ZwglZdfxfbServiceImpl extends ServiceImpl<ZwglZdfxfbMapper, ZwglZdfxfb> implements IZwglZdfxfbService {

    @Autowired
    private ZwglZdfxfbMapper zwglZdfxfbMapper;
    @Autowired
    private ZwglZdfxzbMapper z;

    @Override
    public List<ZwglZdfxfb> selectByMainId(String mainId) {
        return zwglZdfxfbMapper.selectByMainId(mainId);
    }


    @Override
    public Result getzwzs(String dkmc) {

        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkmc", dkmc);
        queryWrapper.like("main_id", "1648899755141898241");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);

        return Result.OK(list);
    }

    @Override
    public Result getzwzsdkone(String dkbh) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkbh", dkbh);
        queryWrapper.like("main_id", "1648899755141898241");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

    @Override
    public Result getzwzsdktwo(String dkbh) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkbh", dkbh);
        queryWrapper.like("main_id", "1648899969613438977");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

    @Override
    public Result getzwzsdkthree(String dkbh) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkbh", dkbh);
        queryWrapper.like("main_id", "1648900192339369986");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

    @Override
    public Result getzwzsdkfour(String dkbh) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkbh", dkbh);
        queryWrapper.like("main_id", "1648934266873475074");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }


    @Override
    public Result getzwzsdkonea(String dkmc,String id) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkmc", dkmc);
        queryWrapper.like("main_id", id);
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

    @Override
    public Result getzwzsdktwob(String dkmc) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkmc", dkmc);
        queryWrapper.like("main_id", "1648899969613438977");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

    @Override
    public Result getzwzsdkthreec(String dkmc) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkmc", dkmc);
        queryWrapper.like("main_id", "1648900192339369986");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

    @Override
    public Result getzwzsdkfourd(String dkmc) {
        QueryWrapper<ZwglZdfxfb> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dkmc", dkmc);
        queryWrapper.like("main_id", "1648934266873475074");
        List<ZwglZdfxfb> list = zwglZdfxfbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }


    @Override
    public List<Map<String, Object>> getsum(){
        List<Map<String, Object>> aggregatedResults = new ArrayList<>();
        QueryWrapper<ZwglZdfxzb> queryWrapper = new QueryWrapper<>();
        List<ZwglZdfxzb> mainTableEntities = z.selectList(queryWrapper);
        ZwglZdfxzb zwglZdfxzb = new ZwglZdfxzb();
        for (ZwglZdfxzb entity : mainTableEntities) {
            String mainId = entity.getId();
            List<Map<String, Object>> results = zwglZdfxfbMapper.selectTotalSumGroupedByDj(mainId);

            // 在添加之前筛选出dj为"好"的数据
            for (Map<String, Object> result : results) {
                if ("好".equals(result.get("dj"))) {
                    zwglZdfxzb.setHao(String.valueOf(result.get("total_sum")));
                    zwglZdfxzb.setId(String.valueOf(result.get("main_id")));
                } else if ("较好".equals(result.get("dj"))) {
                    zwglZdfxzb.setJh(String.valueOf(result.get("total_sum")));
                    zwglZdfxzb.setId(String.valueOf(result.get("main_id")));
                }else if ("正常".equals(result.get("dj"))) {
                    zwglZdfxzb.setZc(String.valueOf(result.get("total_sum")));
                    zwglZdfxzb.setId(String.valueOf(result.get("main_id")));
                }else if ("较差".equals(result.get("dj"))) {
                    zwglZdfxzb.setJc(String.valueOf(result.get("total_sum")));
                    zwglZdfxzb.setId(String.valueOf(result.get("main_id")));
                }else if ("差".equals(result.get("dj"))) {
                    zwglZdfxzb.setCha(String.valueOf(result.get("total_sum")));
                    zwglZdfxzb.setId(String.valueOf(result.get("main_id")));
                }else{
                    System.out.println("错误");
                }
                z.updateById(zwglZdfxzb);
            }
        }
        return aggregatedResults;
    }


}
